package com.ruoyi.operation.order.depot.servive.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.operation.order.depot.domain.Depot;
import com.ruoyi.operation.order.depot.mapper.IDepotMapper;
import com.ruoyi.operation.order.depot.servive.IDepotService;
import com.ruoyi.operation.order.util.ExportExcelUtil;
import com.ruoyi.project.system.role.mapper.RoleMapper;
import com.ruoyi.project.system.user.mapper.UserMapper;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Author king
 * @create 2019/4/15 11:09
 */
@Service
public class DepotServiceImpl implements IDepotService {

    @Autowired
    private IDepotMapper iDepotMapper;

    @Autowired
    private RoleMapper iRoleMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有的仓库管理的数据
     * @param depot
     * @return
     */
    @Override
    public List<Depot> allDepot(Depot depot) {
        return iDepotMapper.allDepot(depot);
    }


    /**
     * 导出订单信息
     * @param depot
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void exportDepot(Depot depot, HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 获取当前登陆人的id
        String getUserId = ShiroUtils.getLoginId();
        // 更具当前登陆人获取roleId
        List<String> getRoleId = iRoleMapper.getRoleId(getUserId);
//        if (getRoleId.equals("1") || getRoleId.equals("2")) {
//            // 此时是超级管理员和鹰之队的管理员
//            depot.setType("1");
//        } else if (getRoleId.equals("3")) {
//            depot.setType("2");
//            // 根据id获取对应的部门id
//            String getDeptId = userMapper.getDeptId(getUserId);
//            depot.setDeptId(getDeptId);
//        }
        for (String s : getRoleId){
            if (s.equals("3")) {
                depot.setType("2");
                // 根据id获取对应的部门id
                String getDeptId = userMapper.getDeptId(getUserId);
                depot.setDeptId(getDeptId);
            }
        }

        List<Depot> depotList = iDepotMapper.allDepot(depot);
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建导的Excel表的名字
        //sheet名
        HSSFSheet sheet = workbook.createSheet("仓库信息表");
        // 设置导出文件的名字和导出的格式
        String fileName = "仓库信息表"+".xls";
        String agent = request.getHeader("USER-AGENT");
        if(agent != null && agent.toLowerCase().indexOf("firefox") > 0)
        {
            fileName = "=?UTF-8?B?" + (new String(Base64Utils.encodeToString(fileName.getBytes("UTF-8")))) + "?=";
        } else {
            fileName =  java.net.URLEncoder.encode(fileName, "UTF-8");
        }
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 新增数据行，设置单元格数据
        int rowNum  = 1;
        // 定义导出Excel的表格
        String[] title = {"录入时间","平台","店铺","产品名称","订单号","审核状态","备注"};
        String[] colList = {"createDate","deptName","shopName","productName","formNum","formStatus","remark"};
        HSSFRow row = sheet.createRow(0);
        // 在Excel表中添加表头
        for (int i = 0;i<title.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(title[i]);
            cell.setCellValue(text);
        }
        // 在表中存放查询到的数据到对应的列
        for (Depot c: depotList){
            HSSFRow row1 = sheet.createRow(rowNum);
            int j = 0;
            for(String s:colList){
                row1.createCell(j).setCellValue((String)getGetMethod(c, s));
                j++;
            }
            rowNum++;
        }
        ExportExcelUtil.outPut(response,workbook,fileName);
    }
    /**
     * 根据属性，获取get方法
     * @param ob 对象
     * @param name 属性名
     * @return
     * @throws Exception
     */
    private Object getGetMethod(Object ob , String name)throws Exception{
        Method[] m = ob.getClass().getMethods();
        for(int i = 0;i < m.length;i++){
            if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
                return m[i].invoke(ob);
            }
        }
        return null;
    }

}
